//
//  FoundationAdditions.m
//  Derived from CMSC498i Lab 5 resources
//

#import "FoundationAdditions.h"
#define HYDRANTS_DB_DIRECTORY @"/tmp/hydrants/"
#define HYDRANTS_DB @"hydrant.sqlite"
#define DEBUG_USE_FAKE_DOCUMENTS_DIRECTORY 1
// If Xcode is giving you a hard time and not preserving your documents between launches, you may need 
// to set DEBUG_USE_FAKE_DOCUMENTS_DIRECTORY to 1 so that you always end up saving documents to the
// same location while testing in the simuator

NSString *NSUserDocumentDirectory(void)
{
#if DEBUG_USE_FAKE_DOCUMENTS_DIRECTORY // [ 
    NSFileManager *fm = [NSFileManager defaultManager];
	//NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"hydrant" ofType:@"sqlite"]; 
    NSString *defaultStorePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:HYDRANTS_DB];
	NSString *path = [NSString stringWithFormat:HYDRANTS_DB_DIRECTORY, nil];
    if (![fm fileExistsAtPath:path]) {
        [fm createDirectoryAtPath:path attributes:nil];
		 path=[path stringByAppendingString:HYDRANTS_DB];
		[fm copyItemAtPath:defaultStorePath toPath:path error:NULL];
    }else{
		path=[path stringByAppendingString:HYDRANTS_DB];
	}
    return path; 
#else
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [paths objectAtIndex:0];
#endif // ] DEBUG_USE_FAKE_DOCUMENTS_DIRECTORY
}
